var token
const pageSize = 10

function requestAPI(api, params, pageIndex) {
	return new Promise((resolve, reject) => {
		let data = {
			"head": {
				"version": "v1",
				"screenWidth": 375,
				"accesstoken": getToken()
			},
			"body": params || {}
		}
		
		if (pageIndex) {
			data.head['pageCount'] = pageIndex;
			data.head['pageSize'] = pageSize;
		}
		
		console.log("请求参数: ", data);
		
		uni.request({
			url: api,
			method: 'POST',
			data: data,
			success: res => {
				uni.hideLoading();
				let statusCode = res.data.head.status;
				let message = res.data.head.message;
				if (statusCode == '200') {
					console.log("res.data: ",res.data);
					resolve(res.data);
				} else {
					msg(message);
				}
				
			},
			fail: (error) => {
				msg('网络异常');
				
				console.log("error.message: ", error.message);
			},
			complete: () => {
			}
		})
	}); 
}

function getToken() {
	if (!sshelper.token) {
		sshelper.token = uni.getStorageSync('userInfo').token;
	}

	return sshelper.token;
}

const msg = (title, duration=1500, mask=false, icon='none')=>{
	//统一提示方便全局修改
	if(Boolean(title) === false){
		return;
	}
	
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

const sshelper = {
	requestAPI: requestAPI
}

module.exports = sshelper
